package org.mspring.mlog.web.freemarker.directive;

import java.io.IOException;
import java.util.Map;

import org.mspring.mlog.core.ServiceFactory;
import org.mspring.mlog.utils.WebUtils;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.freemarker.DirectiveUtils;
import org.mspring.platform.web.freemarker.directive.AbstractDirectiveModel;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 用于生成SpaceUrl
 * 
 * @author Gao Youbo
 * @since 2013年7月8日
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class UserSpaceUrlDirectiveModel extends AbstractDirectiveModel {

    @Override
    public void exec(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        // TODO Auto-generated method stub
        String name = DirectiveUtils.getString("name", params);
        if (StringUtils.isBlank(name)) {
            Long id = DirectiveUtils.getLong("id", params);
            if (id != null) {
                name = ServiceFactory.getUserService().getNameById(id);
            }
        }
        String result = WebUtils.getContextPath() + "/u/" + name;
        env.getOut().append(result);
    }

}
